package com.study.controller;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;

import org.eclipse.jdt.internal.compiler.env.IModule.IService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.study.model.pojo.Inbound;
import com.study.model.pojo.Intype;
import com.study.model.pojo.Outbound;
import com.study.model.pojo.Product;
import com.study.model.pojo.Warehouse;
import com.study.services.IntypeService;
import com.study.services.OutboundService;
import com.study.services.ProductService;
import com.study.services.WarehouseService;

@RestController
@CrossOrigin
public class OutboundController {
	@Autowired
	OutboundService oService;
	@Autowired
	IntypeService iService;
	@Autowired
	WarehouseService wService;
	@Autowired
	ProductService pService;

	/**
	 * 查询出库信息
	 * 
	 * @return
	 */
	@RequestMapping("/findoutbound")
	public JSONObject findoutbound(String intName) {
		JSONObject json = new JSONObject();
		JSONArray json1 = new JSONArray();
		JSONArray json2 = new JSONArray();
		JSONArray json3 = new JSONArray();
		JSONArray json4 = new JSONArray();
		System.out.println("111111");
		// 查询出库表
		if (intName == null || intName.length() == 0 || intName.equals("查询全部")) {
			List<Outbound> list = oService.findAll();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			for (Outbound o : list) {
				JSONObject jo = new JSONObject();
				jo.put("outId", o.getOutId());
				jo.put("proId", o.getProduct().getProName());
				jo.put("saleId", o.getSaleId());
				jo.put("warName", o.getWarName());
				jo.put("intName", o.getIntName());
				jo.put("outNum", o.getOutNum());
				jo.put("outKind", o.getOutKind());
				jo.put("outUse", o.getOutUse());
				jo.put("outDate", sdf.format(o.getOutDate()));
				jo.put("outNote", o.getOutNote());
				jo.put("outStatic", o.getOutStatic());
				json1.add(jo);
			}
		}else {
			List<Outbound> list=oService.findByintName(intName);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			for (Outbound o : list) {
				JSONObject jo = new JSONObject();
				jo.put("outId", o.getOutId());
				jo.put("proId", o.getProduct().getProName());
				jo.put("saleId", o.getSaleId());
				jo.put("warName", o.getWarName());
				jo.put("intName", o.getIntName());
				jo.put("outNum", o.getOutNum());
				jo.put("outKind", o.getOutKind());
				jo.put("outUse", o.getOutUse());
				jo.put("outDate", sdf.format(o.getOutDate()));
				jo.put("outNote", o.getOutNote());
				jo.put("outStatic", o.getOutStatic());
				json1.add(jo);
			}
		}
		// 查询出库类型
		JSONObject jo1 = new JSONObject();
		jo1.put("intId", "int0002");
		jo1.put("intName", "查询全部");
		json2.add(jo1);
		List<Intype> list2 = iService.findByintType("出库");
		for (Intype intype : list2) {
			JSONObject jo = new JSONObject();
			System.out.println(intype.getIntName());
			jo.put("intId", intype.getIntId());
			jo.put("intName", intype.getIntName());
			jo.put("intType", intype.getIntType());
			json2.add(jo);
		}
		// 查询所有仓库名称
		List<Warehouse> list3 = wService.findAll();
		for (Warehouse w : list3) {
			JSONObject jo = new JSONObject();
			jo.put("warId", w.getWarId());
			jo.put("warName", w.getWarName());
			jo.put("warKind", w.getWarKind());
			json3.add(jo);
		}
		//查询所有产品
		List<Product> list4=pService.findAll();
		for (Product p : list4) {
			JSONObject jo = new JSONObject();
			jo.put("proId", p.getProId());
			jo.put("proName", p.getProName());
			json4.add(jo);
		}
		json.put("outbound", json1);
		json.put("intype", json2);
		json.put("warehouse", json3);
		json.put("product", json4);
		return json;
	}

	/**
	 * 新增出库
	 * 
	 * @param outbound 出库信息
	 * @param proId    产品名称
	 * @return
	 */
	@RequestMapping("/addoutbound")
	public JSONObject addoutbound(Outbound outbound, String proId) {
		JSONObject json = new JSONObject();
		outbound.setOutId("out0004");
		outbound.setOutStatic("未审核");
		// 通过前台参数查询产品id
		String proid = pService.findByproName(proId);
		// 装入map集合中
		HashMap map = new HashMap<>();
		map.put("outId", outbound.getOutId());
		map.put("proId", proid);
		map.put("saleId", outbound.getSaleId());
		map.put("warName", outbound.getWarName());
		map.put("intName", outbound.getIntName());
		map.put("outNum", outbound.getOutNum());
		map.put("outKind", outbound.getOutKind());
		map.put("outUse", outbound.getOutUse());
		map.put("outNote", outbound.getOutNote());
		map.put("outStatic", outbound.getOutStatic());
		String returnString = oService.outbound(map, proid, outbound.getWarName(), outbound.getOutNum());
		json.put("ans", returnString);
		return json;
	}

	/**
	 * 修改出库信息
	 * 
	 * @param outbound 出库信息
	 * @param proId    出库产品名称
	 * @return
	 */
	@RequestMapping("/editoutbound")
	public JSONObject editoutbound(Outbound outbound, String proId) {
		JSONObject json = new JSONObject();
		// 通过前台参数查询产品id
		String proid = pService.findByproName(proId);
		// 装入map集合中
		HashMap map = new HashMap<>();
		map.put("outId", outbound.getOutId());
		map.put("proId", proid);
		map.put("saleId", outbound.getSaleId());
		map.put("warName", outbound.getWarName());
		map.put("intName", outbound.getIntName());
		map.put("outNum", outbound.getOutNum());
		map.put("outKind", outbound.getOutKind());
		map.put("outUse", outbound.getOutUse());
		map.put("outNote", outbound.getOutNote());
		oService.editOutbound(map);// 修改信息
		json.put("ans", "y");
		return json;
	}

	/**
	 * 删除出库信息
	 * 
	 * @param outId 出库编号
	 * @return
	 */
	@RequestMapping("/deloutbound")
	public JSONObject deloutbound(String outId) {
		JSONObject json = new JSONObject();
		oService.delOutbound(outId);
		json.put("ans", "y");
		return json;
	}

	/**
	 * 审核通过
	 * 
	 * @param outId 出库编号
	 * @return
	 */
	@RequestMapping("/passout")
	public JSONObject passout(String outId) {
		JSONObject json = new JSONObject();
		oService.pass(outId);
		json.put("ans", "y");
		return json;
	}

	@RequestMapping("outbound")
	public ModelAndView add() {
		HashMap map = new HashMap<>();
		map.put("outId", "out0004");
		map.put("proId", "pro0005");
		map.put("saleId", "怡宝");
		map.put("warName", "北京仓库");
		map.put("intName", "订单出库");
		map.put("outNum", 300);
		map.put("outKind", "自费运输");
		map.put("outUse", "大老板");
		map.put("outNote", "好喝解渴");
		map.put("outStatic", "待处理");
		oService.outbound(map, "怡宝", "11", 300);
		ModelAndView mv = new ModelAndView();
		return mv;
	}
}
